package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.Report;
import com.tanhua.dubbo.api.mongo.ReportApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service(timeout = 20000)
public class ReportApiIpml implements ReportApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Report findOne(ObjectId qid, Long userId) {
        Query query = new Query(Criteria.where("qId").is(qid).and("userId").is(userId));
        return mongoTemplate.findOne(query,Report.class);
    }

    @Override
    public Report save(Report report) {
        Report r1 = mongoTemplate.save(report);
        return r1;
    }

    @Override
    public List<Report> findByUser(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.with(Sort.by(Sort.Order.desc("updated")));
        List<Report> one = mongoTemplate.find(query, Report.class);
        return one;
    }

    @Override
    public Report findById(ObjectId reportId) {
        return mongoTemplate.findById(reportId,Report.class);
    }

    //跟我相似
    @Override
    public List<Report> findSimilar(ObjectId reportId) {
        Report report = findById(reportId);
        Integer score = report.getScore();
        Query query = null;
        if(score < 20){
            query = new Query(Criteria.where("score").gte(0).andOperator(Criteria.where("score").lte(score+15)));
        }else{
            query = new Query(Criteria.where("score").lte(score + 15).orOperator(Criteria.where("score").gte(score - 15)));
        }

        List<Report> reports = mongoTemplate.find(query, Report.class);
        return reports;
    }

    @Override
    public void removeOldReport(Long userId) {
        removeOldReportByQId(userId, "6118815ace2c1224a06916ab");
        removeOldReportByQId(userId, "6118815ace2c1224a06916ad");
        removeOldReportByQId(userId, "6118815ace2c1224a06916ae");
    }

    public void removeOldReportByQId(Long userId,String qnId){
        Query q1 = new Query(Criteria.where("userId").is(userId).and("qId").is(new ObjectId(qnId)));
        List<Report> reports = mongoTemplate.find(q1, Report.class);
        Long updated = 0L;
        if(reports != null){
            for (Report report : reports) {
                Long u1 = report.getUpdated();
                if(u1 > updated){
                    updated = u1;
                }
            }
        }
        Query query = new Query(Criteria.where("userId").in(userId).and("updated").lt(updated).and("qId").is(new ObjectId(qnId)));
        mongoTemplate.remove(query,Report.class);
    }

    @Override
    public String findByQnId(String Qnid,Long userId) {
        Query query = new Query(Criteria.where("qId").is(new ObjectId(Qnid)).and("userId").is(userId));
        Report one = mongoTemplate.findOne(query, Report.class);
        if(one == null){
            return null;
        }
        return one.getId().toString();
    }
}
